package com.oodso.say.utils;

import android.os.Environment;
import android.os.StatFs;

/**
 * 项目名称：Sosoliuda
 * 类描述：
 * 创建人：资拥
 * 创建时间：16/9/22 下午2:09
 * 修改人：资拥
 * 修改时间：16/9/22 下午2:09
 * 修改备注：
 */
public class StorageUtils {


    private StorageUtils() {
    }

    /**
     * 是否挂载SD卡
     */
    public static boolean hasSD() {
        // return false;
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
    }

    /**
     * SD卡根路径
     *
     * @return
     */
    public static String getSdRoot() {
        return hasSD() ? Environment.getExternalStorageDirectory().getAbsolutePath() : null;
    }

    /**
     * 判断SD卡是否已满了
     *
     * @return
     */
    public static boolean SDIsFull() {
        return hasSD() && (getAvailableStore(getSdRoot()) < 1024 * 32);
    }

    /**
     * 返回单位是 B
     *
     * @param filePath
     * @return
     */
    public static long getAvailableStore(String filePath) {
        StatFs statFs = new StatFs(filePath);
        long blocSize = statFs.getBlockSize();
        long availaBlock = statFs.getAvailableBlocks();
        return availaBlock * blocSize;
    }

    /**
     * 取得手机sd卡剩余空间
     */
    public static long getSDAvailableStore() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            if (Environment.getExternalStorageDirectory().canWrite()) {
                return getAvailableStore(Environment
                        .getExternalStorageDirectory().getPath());
            }
        }
        return -1;
    }

    /**
     * 取得手机存储剩余空间
     */
    public static long getInternalAvailableStore() {
        return getAvailableStore(Environment.getDataDirectory()
                                            .getAbsolutePath());
    }
}
